package com.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.domain.IssueCommentResponse;
import com.domain.IssueCommentResponseGrandson;
import com.domain.UserLikeWithSonComment;
import com.domainVO.PageInfoVO;
import com.domainVO.SonCommentUserVO;
import com.mapper.helping.UserLikeWithSonCommentMapper;
import com.mapper.helping.IssueCommentResponseGrandsonMapper;
import com.mapper.helping.IssueCommentResponseMapper;
import com.mapper.living.UserDao;
import com.service.IIssueCommentResponseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.util.UserUtil;
import living.com.domain.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 大忽悠
 * @since 2022年02月08日
 */
@RequiredArgsConstructor
@Service
public class IssueCommentResponseServiceImpl extends ServiceImpl<IssueCommentResponseMapper, IssueCommentResponse> implements IIssueCommentResponseService {
    private final IssueCommentResponseMapper issueCommentResponseMapper;
    private final UserDao userDao;
    private final IssueCommentResponseGrandsonMapper issueCommentResponseGrandsonMapper;
    private final IssueCommentResponseGrandsonServiceImpl iIssueCommentResponseGrandsonService;
    private final UserLikeWithSonCommentMapper userLikeWithSonCommentMapper;
    /**
     * 只有评论发布者和当前版主才有资格删除当前儿子评论
     */
    @Override
    public Boolean logicDelById(Integer son_comment_id) {
        //只有自己和版主才有权删除评论
        IssueCommentResponse issueCommentResponse = issueCommentResponseMapper.selectById(son_comment_id);
        Assert.isTrue(
                 //版主
                UserUtil.getUserId()==issueCommentResponse.getToUserId()||
                //自己
                UserUtil.getUserId()==issueCommentResponse.getFromUserId(),"无权删除当前评论");
        return issueCommentResponseMapper.logicDelById(son_comment_id);
    }

    @Override
    public Boolean WeiXinDel(Integer son_comment_id) {
        return iIssueCommentResponseGrandsonService.logicDelById(son_comment_id);
    }

    /**
     * 传入子评论集合
     * @param records
     * @return
     */
    public List<SonCommentUserVO> commonOperation(List<IssueCommentResponse> records,Integer grant_son_startPage,Integer grant_son_pageSize)
    {
        //每个子评论带当前用户信息
        List<SonCommentUserVO> sonCommentUserVOS = records.stream().map(son -> {
            //判断是否是匿名用户---如果是匿名回答,不想知道是谁发布的评论
            //设置发布者为null
            if(son.getFromUserId()!=0&&son.getToUserId()!=0)
            {
                User byId = userDao.publishWithAttention(son.getFromUserId(),UserUtil.getUserId());
                son.setPublisher(byId);
            }
            //如果当前子评论被逻辑删除了,进行处理
            if(son.getIsDeleted()==1)
            {
                son.setContent(null);
            }
            //当前用户是否给当前儿子点赞了
            UserLikeWithSonComment one = userLikeWithSonCommentMapper.selectOne(new QueryWrapper<UserLikeWithSonComment>()
                    .eq("user_id", UserUtil.getUserId())
                    .eq("son_comment_id", son.getId()));
            if(one!=null)
            {
                son.setIsLike(1);
            }
            else
            {
                son.setIsLike(0);
            }
            //查询当前子评论的指定数目的热门孙子评论
            Page<IssueCommentResponseGrandson> issueCommentResponseGrandsonPage = issueCommentResponseGrandsonMapper.selectPage(new Page<>(grant_son_startPage, grant_son_pageSize)
                    , new QueryWrapper<IssueCommentResponseGrandson>().eq("father_comment_id", son.getId()).orderByDesc("id"));
            List<IssueCommentResponseGrandson> records1 = issueCommentResponseGrandsonPage.getRecords();
            //过滤被删除的用户,并进行匿名设置
            List<IssueCommentResponseGrandson> grantSonCommentUserVOS = iIssueCommentResponseGrandsonService
                    .commonOperation(records1);
            return SonCommentUserVO.builder().father(son).sons(grantSonCommentUserVOS).build();
        }).collect(Collectors.toList());
        return sonCommentUserVOS;
    }

    /**
     * <P>
     *     儿子评论携带指定数目孙子评论
     * </P>
     * @param father_comment_id
     * @return
     */
    @Override
    public Map<String, Object> getSonCommentUnderTargetFatherComment
    (Integer father_comment_id, Integer son_startPage, Integer son_pageSize,
    Integer grant_son_startPage,Integer grant_son_pageSize) {
        Page<IssueCommentResponse> page=new Page<>(son_startPage,son_pageSize);
        Page<IssueCommentResponse> responsePage = issueCommentResponseMapper.selectPage(page, new QueryWrapper<IssueCommentResponse>()
                .eq("father_comment_id", father_comment_id).orderByDesc(Arrays.asList("like_sum","response_sum","id")));
        List<SonCommentUserVO> sonCommentUserVOS = commonOperation(responsePage.getRecords(),grant_son_startPage,grant_son_pageSize);
        return PageInfoVO.builder().autoWired(page,sonCommentUserVOS);
    }
}
